﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TouchRC.MetadataCaching.Extensions
{
    public static class FileInfoExtensions
    {
        public static FileInfo GetTvSeriesXml(this DirectoryInfo dir)
        {
            string newPath = Path.Combine(dir.FullName, "series.xml");

            return new FileInfo(newPath);
        }

        public static FileInfo GetMyMoviesXml(this DirectoryInfo dir)
        {
            string newPath = Path.Combine(dir.FullName, "mymovies.xml");

            return new FileInfo(newPath);
        }

        public static FileInfo GetFolderPoster(this DirectoryInfo dir)
        {
            string newPath = Path.Combine(dir.FullName, "folder.jpg");

            return new FileInfo(newPath);
        }

        public static FileInfo GetFolderBackdrop(this DirectoryInfo dir)
        {
            string newPath = Path.Combine(dir.FullName, "backdrop.jpg");

            return new FileInfo(newPath);
        }

        public static string GetFileNameWithoutExtension(this FileInfo file)
        {
            return Path.GetFileNameWithoutExtension(file.Name);
        }

        public static void CheckDirectoryCreation(this DirectoryInfo dir)
        {
            if (!Directory.Exists(dir.FullName))
                Directory.CreateDirectory(dir.FullName);
        }

        public static List<DirectoryInfo> GetVirtualCacheDirectories(this DirectoryInfo dir)
        {
            List<DirectoryInfo> retVal = new List<DirectoryInfo>();

            DirectoryInfo[] dirs = dir.GetDirectories();
            foreach (DirectoryInfo potential in dirs)
            {
                if (potential.GetFiles("Summary.xml").Count() == 1)
                    retVal.Add(potential);
            }
            retVal.Sort((a, b) => a.Name.CompareTo(b.Name));
            return retVal;
        }
    }
}
